const nodemailer = require('nodemailer');
// const { service_ip } = require('../config');

const transporter = nodemailer.createTransport({
    host: 'smtp.qq.com', //QQ邮箱的服务器
    port: 465, //端口号
    secure: true, //465为true,其他为false
    auth: {
        user: '577914237@qq.com', // 自己的邮箱
        // pass: 'mchpoftlbaysbcjd', // 授权码
        pass: 'qquaynlvkejlbejd', // 授权码
    },
});


/**
 * 注册用户时发送邮箱
 */
exports.sendRegisterEmail = ({email,verify_key}) => {
    console.log('email', email)
    // const url = `${service_ip}/regiter_success?id=${user_id}&verify_key=${verify_key}`;
    const params = {
        from: 'Technique<577914237@qq.com>', // 收件人显示的发件人信息
        to: email, // 目标邮箱号
        subject: '注册新用户',
        html: emailTelempateF(verify_key),
    };
    return sendMsg(params);
};

/**
 * 找回密码时发送校验码
 * @param {*} params
 */
exports.sendCode = ({email, verify_key}) => {
    const params = {
        from: '梁朝伟<xxxxxxxx@qq.com>', // 收件人显示的发件人信息
        to: email, // 目标邮箱号
        subject: '找回密码',
        html: `邮箱验证码:${verify_key}`,
    };
    return sendMsg(params);
};

/**
 * 发送消息
 */
const sendMsg = (params) => {
    return new Promise((resolve) => {
        transporter.sendMail(params, (err, data) => {
            resolve(null);
            transporter.close(); //发送完毕后关闭
        });
    });
};

/**
 * 模版
 * */
const emailTelempateF = (verify_key) => {
    return `
<div style="width:100%;max-width: 440px; padding: 0 20px;">
    <div style="max-width:100%;background-color: #f1f1f1; padding: 20px 16px; font-weight: bold;font-size: 20px;color: rgb(22,24, 35)">
      验证码
    </div>
    <div style="max-width:100%;background-color: #f8f8f8; padding: 24px 16px;font-size: 17px;color: rgba(22,24, 35, 0.75);line-height: 20px;">
      <p style="margin-bottom:20px;">若要验证您的帐号，请在 Technique 中输入此验证码：</p>
      <p style="margin-bottom:20px;color: rgb(22,24,35);font-weight: bold;">${verify_key}</p>
      <p style="margin-bottom:20px;">验证码将于 48 小时后过期。</p>
      <p style="margin-bottom:20px;">如果您未申请此验证码，请忽略此邮件。</p>
    </div>
    <div style="max-width:100%;padding: 40px 16px 20px;font-size: 15px;color: rgba(22, 24, 35, 0.5);line-height:18px;">
      <div>这是一封自动产生的电子邮件。我们不会监控对此电子邮件的回复。</div>
    </div>
  </div>
`
}

